package com.heima.wemedia.feign;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//相当于把下面的controller接口注册到leadnews-feign-api里面，以提供给其它微服务调用
//其它微服务调用leadnews-feign-api里的服务的方法：
// 1.谁调用了，谁就在其启动类里添加 @EnableFeignClients("com.heima.apis")
// 2.在Impl类里@Autowired IWemediaClient wemediaClient;
// 3.调用wemediaClient.findWmUserByName(xxx);

//和 leadnews-feign-api包里的IWemediaClient对接
@RestController
public class WemediaClient implements IWemediaClient {

    @Autowired
    private WmUserService wmUserService;

    /*
    * 根据用户名查询自媒体用户信息
     */
    @Override
    @GetMapping("/api/v1/user/findByName/{name}")
    public WmUser findWmUserByName(@PathVariable("name") String name) {
        return wmUserService.getOne(Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, name));
    }

    /**
     * 新增自媒体用户信息
     */
    @Override
    @PostMapping("/api/v1/wm_user/save")
    public ResponseResult saveWmUser(@RequestBody WmUser wmUser) {
        wmUserService.save(wmUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Autowired
    private WmChannelService wmChannelService;

    /**
     * 查询所有频道
    *///article微服务里的HotArticleServiceImpl里的cacheTagToRedis(为每个频道缓存30条分值较高的文章)需要调用
    @GetMapping("/api/v1/channel/list")
    @Override
    public ResponseResult getChannels() {
        return wmChannelService.findAll();
    }
}
